package com.ldc.framework.fragment

import android.os.Bundle
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMFragment
import com.ldc.commonres.uts.TagUts
import com.ldc.commonres.views.StateViewLayout
import com.ldc.framework.R
import com.ldc.framework.activity.ProjectsListActivity
import com.ldc.framework.adapter.ProjectAdapter
import com.ldc.framework.databinding.FrameworkFragmentProjectsBinding
import com.ldc.framework.model.ProjectClassModel
import com.ldc.framework.viewmodel.FrameworkViewModelBase


class ProjectsFragment : VBMFragment<FrameworkFragmentProjectsBinding, FrameworkViewModelBase>() {
    private var projectAdapter: ProjectAdapter? = null

    companion object {
        private const val REFRESH_CODE = 0x1234
        fun newInstance(): ProjectsFragment = ProjectsFragment().also {
            it.arguments = Bundle()
        }

    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                projectClassList()
            }
        }
        return super.processMessage(msg)
    }

    override fun initStateViewLayout(): StateViewLayout? =
        StateViewLayout.init(this, R.id.content_view)

    override fun main() {
        initAdapter()
        viewBind?.contentView?.refreshView?.also { rf ->
            rf.setOnRefreshListener {
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
            rf.setEnableLoadMore(false)

        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)
        addOnReTryListener(object : OnRowListener<Int> {
            override fun onRow(v: View, data: Int) {
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
        })
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FrameworkFragmentProjectsBinding =
        FrameworkFragmentProjectsBinding.inflate(inflater, container, false)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    private fun projectClassList() {
        showLoadingView()
        viewModel?.projectClassList()?.observe(this) {
            showContentView()
            viewBind?.contentView?.refreshView?.also { rf ->
                if (rf.state.isOpening) {
                    rf.finishRefresh()
                }
            }
            if (ApiUtils.isSuccess(it)) {
                if (it.data.isNullOrEmpty()) {
                    showEmptyView()
                } else {
                    projectAdapter?.newData(it.data)
                }
            } else {
                showErrorView()
            }
        }
    }


    private fun initAdapter() {
        projectAdapter = ProjectAdapter().also {
            it.addOnRowListener(object : OnRowListener<ProjectClassModel.ProjectClassModelItem> {
                override fun onRow(v: View, data: ProjectClassModel.ProjectClassModelItem) {
                    ProjectsListActivity.start(this@ProjectsFragment, data.name, "${data.id}")
                }
            })
        }
        viewBind?.contentView?.rv?.recyclerview?.also {
            it.adapter = projectAdapter
            it.setHasFixedSize(true)
            it.layoutManager = TagUts.getLayoutManager(it.context)
        }
    }


}